// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import { orderPayApi, orderCancel } from "@/api/order.js";
export default {
  data() {
    return {
      isBuy: false, //是否可以点击购买
    };
  },
  onShow() {},
  methods: {
    /**
     * 调支付订单接口后的操作
     * @param {Object} res 接口返回的值
     * @param {Object} orderNo 订单号
     * @param {Object} productType 商品类型
     * @param {Object} fromType 页面来源
     * @param {Object} payType 支付方式
     * @param {Object} payPrice  支付金额
     */
    handleOrderPay(res, orderNo, productType, fromType, payType, payPrice) {
      let jsConfig = res.data.jsConfig;
      let goPages = "";
      if (fromType === "svip") {
        goPages = `/pages/goods/order_pay_status/index?order_id=${orderNo}&payType=${payType}&payPrice=${payPrice}&fromType=${fromType}`;
      } else {
        goPages = "/pages/goods/order_pay_status/index?order_id=" + orderNo;
      }
      switch (res.data.payType) {
        case "weixin":
          uni.hideLoading();
          this.weixinPay(
            jsConfig,
            orderNo,
            goPages,
            productType,
            fromType,
            res.data.orderNo
          );
          break;
      }
    },
    //微信支付
    weixinPay(jsConfig, orderNo, goPages, productType, fromType, newOrderNo) {
      uni.requestPayment({
        timeStamp: jsConfig.timeStamp,
        nonceStr: jsConfig.nonceStr,
        package: jsConfig.packages,
        signType: jsConfig.signType,
        paySign: jsConfig.paySign,
        ticket: productType === "normal" ? null : jsConfig.ticket,
        success: function (ress) {
          setTimeout((res) => {
            uni.hideLoading();
            wx.navigateBackMiniProgram({
              extraData: {
                shouldRedirect: true,
                targetPath: this.goPages, // 回传路径标识
              },
            });
          }, 500);
        },
        fail: function (e) {
          uni.hideLoading();
          orderCancel(newOrderNo).then((res) => {
            uni.showToast({ title: "取消支付", icon: "none" });
            wx.navigateBackMiniProgram({
              extraData: {
                shouldRedirect: true,
                targetPath: this.goPages, // 回传路径标识
              },
            });
          });
        },
        complete: function (e) {
          uni.hideLoading();
          //关闭当前页面跳转至订单状态
          if (e.errMsg == "requestPayment:cancel") {
            orderCancel(newOrderNo).then((res) => {
              uni.showToast({ title: "取消支付", icon: "none" });
              wx.navigateBackMiniProgram({
                extraData: {
                  shouldRedirect: true,
                  targetPath: this.goPages, // 回传路径标识
                },
              });
            });
          }
        },
      });
    },

    /**
     * 跳入支付收银台页面
     * @param {Object} secondType 二级订单类型
     * @param {data} res 订单对象
     */
    getToPayment(secondType, data) {
      let url = `/pages/goods/order_payment/index?orderNo=${data.orderNo}&payPrice=${data.payPrice}`;
      // let url = ''
      // if(secondType === this.ProductTypeEnum.Integral){
      // 	url = `/pages/goods/order_payment/index?orderNo=${data.orderNo}&payPrice=${data.payPrice}&fromType=integral`
      // }else{
      // 	url = `/pages/goods/order_payment/index?orderNo=${data.orderNo}&payPrice=${data.payPrice}`
      // }
      uni.redirectTo({
        url: url,
      });
    },

    /**
     * 订单支付
     * @param {Object} orderNo 订单号
     * @param {Object} payChannel  支付渠道
     * @param {Object} payType 支付方式
     * @param {Object} productType 商品类型
     * @param {Object} fromType  页面来源
     * @param {Object} payPrice  支付金额
     */
    changeOrderPay(
      orderNo,
      payChannel,
      payType,
      productType,
      fromType,
      payPrice
    ) {
      let data = {
        orderNo: orderNo,
        payChannel: payChannel,
        appId: "wxba0b11befd4f87fc",
        payType: payType,
        openId: this.openId,
        scene: productType === "normal" ? 0 : 1177,
      };
      uni.hideLoading();
      orderPayApi(data)
        .then((res) => {
          this.handleOrderPay(
            res,
            orderNo,
            productType,
            fromType,
            payType,
            payPrice
          );
        })
        .catch((err) => {
          uni.hideLoading();
          this.isBuy = false;
          uni.showToast({ title: err, icon: "none" });
        });
    },
  },
};
